<template>
    <view class="reply">
        <view class="top">
            <comment-item :item="replyItem" :childState="true" :closeBtn="false"></comment-item>
        </view>
        <view class="list">
            <view class="row" v-for="item in commentList">
                <comment-item plDel="plDel" :childState="true" :item="item"></comment-item>
            </view>
        </view>

        <view>
            <comment-frame @toParant="toParant" :commentObj="commentObj"></comment-frame>
        </view>

    </view>
</template>


<script setup>
    import {
        onUnload
    } from "@dcloudio/uni-app"
    import {
        onMounted,
        reactive,
        ref
    } from "vue";
    const db = uniCloud.database();
    //获取缓存信息
    let replyItem = uni.getStorageSync('commentItem');

    if (!replyItem) {
        uni.navigateBack();
    }
    //回复回调
    function toParant(e) {
        commentList.value = '';
        getcoment();
    }
    //删除回调
    function plDel(e) {
        console.log(e);
        commentList.value = '';
        getcoment();
    }
    const commentList = ref();
    //获取评论
    function getcoment() {
        let cmentTemp = db.collection('quanzi_comment').where(
                `article_id=="${ replyItem.article_id}" && comment_type == 1 && reply_comment_id =="${replyItem._id}"`)
            .orderBy(
                "comment_date desc")
            .getTemp();
        let userTemp = db.collection('uni-id-users').field('_id,username,nickname,avatar_file').getTemp();
        db.collection(cmentTemp, userTemp).limit(5).get().then(res => {
            commentList.value = res.result.data;
            console.log(commentList.value);
        })
    }
    //销毁清除缓存
    onUnload(() => {
        uni.removeStorageSync('commentItem');
    })
    let commentObj = reactive({
        article_id: '',
        comment_type: 1,
        reply_user_id: '',
        reply_comment_id: ''
    })
    onMounted(() => {
        commentObj.article_id = replyItem.article_id;
        commentObj.reply_user_id = replyItem.user_id[0]._id;
        commentObj.reply_comment_id = replyItem._id;
        getcoment();
    })
</script>

<style lang="scss">
    .reply {
        .top {
            padding: 30rpx;
            border-bottom: 15rpx solid #eee;
        }

        .list {
            padding: 30rpx;
            padding-bottom: 120rpx;

            .row {
                padding-bottom: 15rpx;
            }
        }
    }
</style>